package tallerweb.sangucheto.controllers;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.SortedSet;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import tallerweb.sangucheto.beans.forms.VerSanguchetoFormMO;
import tallerweb.sangucheto.model.Ingrediente;
import tallerweb.sangucheto.utils.Sanguchetto;
import tallerweb.sangucheto.utils.Stock;

@Controller
public class DefaultController {

	@RequestMapping(value = "/verSangucheto", method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView verSanguchetoMethod() {
		// vaciar para cuando retorna desde el boton volver
		Sanguchetto.getInstance().vaciar();
		ModelMap modelMap = new ModelMap("modelMap");
		modelMap.put("verSanguchetoFormMO", new VerSanguchetoFormMO());
		modelMap.put("ingredientesStock", Stock.getInstance().listarIngredientesDisponibles());
		ModelAndView model = new ModelAndView("verSangucheto", modelMap);
		return model;
	}

	@RequestMapping(value = "/agregarIngrediente", method = {RequestMethod.POST, RequestMethod.GET })
	public ModelAndView procesarIngredientesMethod(
			@ModelAttribute("verSanguchetoFormMO") VerSanguchetoFormMO verSanguchetoFormMO,
			@RequestParam Map<String, String> requestParams) {
		if (requestParams.get("boton").equals("confirmar")) {

			VerSanguchetoFormMO sanguchetoFinal = new VerSanguchetoFormMO();
			for (Iterator<Ingrediente> iterator = Sanguchetto.getInstance().verIngredientes().iterator(); iterator.hasNext();) {
				Ingrediente ingrediente = (Ingrediente) iterator.next();
				verSanguchetoFormMO.getIngredientes().add(ingrediente);
			}
			for (Iterator<Ingrediente> iterator = Sanguchetto.getInstance().verCondimentos().iterator(); iterator.hasNext();) {
				Ingrediente ingrediente = (Ingrediente) iterator.next();
				verSanguchetoFormMO.getIngredientes().add(ingrediente);
			}
			ModelMap modelMap = new ModelMap("modelMap");
			modelMap.put("sanguchetoFinalFormMO", verSanguchetoFormMO);
			modelMap.put("finalIngredientes",verSanguchetoFormMO.getIngredientes());
			modelMap.put("finalPrecio", Sanguchetto.getInstance().getPrecio());
			return new ModelAndView("sanguchetoFinal", modelMap);
		} else {

			ModelMap modelMap = new ModelMap("modelMap");
			modelMap.put("verSanguchetoFormMO", verSanguchetoFormMO);

			if (verSanguchetoFormMO.getIngrediente().getNombre() != null) {
				if (requestParams.get("boton").equals("agregar"))
					// if
					// (verSanguchetoFormMO.getIngrediente().getTipo().equals(TipoIngrediente.INGREDIENTE))
					Sanguchetto.getInstance().agregarIngrediente(
							verSanguchetoFormMO.getIngrediente());
				else { // es eliminar
					Sanguchetto.getInstance().eliminar(verSanguchetoFormMO.getIngrediente());
					// solo para el debug
					Sanguchetto.getInstance().verCondimentos();
					Sanguchetto.getInstance().verCondimentos();
				}
			}

			// Stock.getInstance().listarIngredientesDisponibles(); //los Set no
			// tienen get element!
			modelMap.put("ingredientesStock", Stock.getInstance().listarIngredientesDisponibles());
			modelMap.put("calculoPrecio", Sanguchetto.getInstance().getPrecio());
			ModelAndView model = new ModelAndView("verSangucheto", modelMap);
			return model;
			// TO-DO redirect
		}

	}

	@RequestMapping(value = "/agregarIngredientes", method = {RequestMethod.POST, RequestMethod.GET })
	public ModelAndView agregarIngredientesMethod(VerSanguchetoFormMO verSanguchetoFormMO) {
		return new ModelAndView("agregarIngredientes", new ModelMap("UnModelMap"));
	}

	// @RequestMapping(value="/verSangucheto", params={"confirmar", "!agregar"},
	// method = {RequestMethod.POST, RequestMethod.GET})
	/*
	 * @RequestMapping(value="/verSangucheto", method = {RequestMethod.POST,
	 * RequestMethod.GET})
	 * 
	 * public ModelAndView verSanguchetoFinalMethod(VerSanguchetoFormMO
	 * verSanguchetoFormMO){ ModelMap modelMap = new ModelMap("UnModelo");
	 * return new ModelAndView("sanguchetoFinal",modelMap); }
	 */

	// ------------------------------------------------------------------------
	/*
	 * ejemplo pag: ejemplo tomado de
	 * http://kaparechi.com/form-handling-using-commandname/
	 */
	//
	/*
	 * @RequestMapping(value= {"/launchInput"}) public ModelAndView
	 * launchInputForm() { //The second parameter in the below constructor will
	 * bind the form parameters to the user object ModelAndView model = new
	 * ModelAndView( "inputForm","user",new UserDomainObject()); return model;
	 * 
	 * }
	 * 
	 * @RequestMapping(value= {"/submitData"}) public ModelAndView
	 * getInputForm(UserDomainObject user) { ModelAndView model = new
	 * ModelAndView( "displayResults","user",user); return model;
	 * 
	 * }
	 * 
	 * 
	 * @RequestMapping(value= {"/input"}) public ModelAndView
	 * inputForm(UserDomainObject user) { ModelAndView model = new ModelAndView(
	 * "displayResults","user",user); return model;
	 * 
	 * }
	 */

	// ------------------------------------------------------------------------
	/*
	 * ejemplo pag:
	 * http://www.javacodegeeks.com/2013/05/spring-mvc-form-handling
	 * -vol-3-checkboxes-processing.html
	 */
	/*
	 * @RequestMapping(value="/football-page") private ModelAndView
	 * footballPage() { //ModelAndView mav = new ModelAndView("football-form");
	 * ModelAndView mav = new ModelAndView("football-page");
	 * 
	 * 
	 * List teams = new ArrayList(); teams.add("Bavaria Munich");
	 * teams.add("Borussia Dortmund"); teams.add("Real Madrid");
	 * teams.add("Barcelona");
	 * 
	 * mav.addObject("teamsList", teams); mav.addObject("footballTeams", new
	 * FootballTeams());
	 * 
	 * return mav; }
	 * 
	 * @RequestMapping(value="/football-result") private ModelAndView
	 * processTeams(@ModelAttribute FootballTeams footballTeams) { ModelAndView
	 * mav = new ModelAndView("football-result"); mav.addObject("footballTeams",
	 * footballTeams); return mav; }
	 */

	@RequestMapping(value = "/altaIngrediente", method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView mostrarAltaIngrediente() {

		ModelMap model = new ModelMap();
		model.put("ingrediente", new Ingrediente());
		return new ModelAndView("altaIngrediente", model);
	}

	@RequestMapping(value = "/CrearIngrediente", method = { RequestMethod.POST,	RequestMethod.GET })
	public ModelAndView mostrarCrearIngrediente(@ModelAttribute("ingrediente") Ingrediente ingrediente) {

		Stock.getInstance().agregarIngrediente(ingrediente);
		ModelMap model = new ModelMap();
		model.put("ingrediente", Stock.getInstance().listarIngredientesDisponibles());
		return new ModelAndView("redirect:altaIngrediente", model);
	}

	@RequestMapping(value = "/verStock", method = { RequestMethod.POST,	RequestMethod.GET })
	public ModelAndView mostrarVerStock() {

		ModelMap model = new ModelMap();
		LinkedList<Integer> numStock = new LinkedList<Integer>();

		for (Ingrediente ing : Stock.getInstance().listarIngredientesDisponibles()) {
			numStock.add(Stock.getInstance().obtenerStockDisponible(ing));
		}

		model.put("ingrediente", Stock.getInstance().listarIngredientesDisponibles());
		model.put("numStock", numStock);

		return new ModelAndView("verStock", model);
	}
}